package com.common.weibolib;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * ClassName:TopicInfoParser
 * Function: TODO ADD FUNCTION
 * Reason:	 TODO 网龙微博API(新版)
 * 
 * @author   chenqian		
 * @version  
 * @since    Ver 1.1
 * @Date	 2014	2014-1-28		下午5:45:50
 *
 * @see 	 
 */

public class TopicInfoParser extends AbstractObjParser<TopicInfo>{

	/**
	 * @n<b>函数名称</b>     :parse
	 * @brief
	 * @see
	 * @since    Ver 1.1
	 * @param  @param json
	 * @param  @return
	 * @param  @throws JSONException
	 * @<b>作者</b>          :  chenqian
	 * @<b>创建时间</b>      :  2014-1-28下午5:46:39      
	*/
	
	@Override
	public TopicInfo parse(JSONObject json) throws JSONException {
		TopicInfo topicInfo = new TopicInfo();
		if(null == json){
			topicInfo.user = new UserInfo();
			return topicInfo;
		}
		topicInfo.tid = json.optLong("tid");
		topicInfo.unit_id = json.optLong("unit_id");
		topicInfo.content = json.optString("content");
		topicInfo.location = json.optString("location");
		topicInfo.type = json.optString("type");
		topicInfo.category = json.optString("category");
		topicInfo.extra_id = json.optString("extra_id");
		topicInfo.root_tid = json.optInt("root_tid", 0);
		topicInfo.post_time = json.optLong("post_time");
		
		topicInfo.weibo_tid = json.optLong("weibo_tid");
		topicInfo.request_id = json.optLong("request_id");
		topicInfo.replys = json.optInt("replys");
		topicInfo.forwards = json.optInt("forwards");
		topicInfo.glances = json.optInt("glances",0);
		topicInfo.praises = json.optInt("praises");
		topicInfo.setPraised(json.optInt("praised"));
		topicInfo.article = json.optString("article");
		topicInfo.user = new UserInfoParser().parse(json.optJSONObject("user"));
		JSONArray attach = json.optJSONArray("attach");
		if(null != attach){
			topicInfo.attach = new AttachListParser().parse(attach);
		}
		JSONArray image = json.optJSONArray("image");
		if(null != image){
			topicInfo.image = new ImageListparser().parse(image);
		}
		if(topicInfo.type.compareTo(TopicInfo.TYPE_FORWARD) == 0
				&& json.optJSONObject("root_topic") != null){
			topicInfo.root_topic = new RootTopicParser().parse(json.optJSONObject("root_topic"));
		}
		
		//增加解析排行榜中的头像信息
		if(topicInfo.category.compareTo(TopicInfo.CATEGORY_RANK) == 0){
		    ArrayList<Long> uidlist = topicInfo.getUidsForRank();
		    int size = uidlist.size();
            if(json.has("ranking_user")){
                topicInfo.rankingUserList = new UserInfoListParser().parse(json.optJSONArray("ranking_user"));
                //防止和extra_id的数量不一致
                if(topicInfo.rankingUserList.size() < size){
                    for(int i = 0, l = size - topicInfo.rankingUserList.size(); i < l; ++i){
                        UserInfo userInfo = new UserInfo();
                        userInfo.uid = uidlist.get(i);
                        topicInfo.rankingUserList.add(userInfo);
                    }
                }
            } else {
                topicInfo.rankingUserList = new UserInfoList();
                for(int i = 0; i < size; ++i){
                    UserInfo userInfo = new UserInfo();
                    userInfo.uid = uidlist.get(i);
                    topicInfo.rankingUserList.add(userInfo);
                }
            }
        }
		return topicInfo;
	}

	/**
	 * @n<b>函数名称</b>     :toJSONObject
	 * @brief
	 * @see
	 * @since    Ver 1.1
	 * @param  @param t
	 * @param  @return
	 * @param  @throws JSONException
	 * @<b>作者</b>          :  Administrator
	 * @<b>创建时间</b>      :  2014-1-28下午5:46:39      
	*/
	
	@Override
	public JSONObject toJSONObject(TopicInfo t) throws JSONException {
		JSONObject json = new JSONObject();
		if(null == t){
			return json;
		}
		json.put("tid", t.tid);
		json.put("unit_id", t.unit_id);
		json.put("group_id", t.group_id);
		json.put("content", t.content);
		json.put("location", t.location);
		json.put("type", t.type);
		json.put("category", t.category);
		json.put("extra_id", t.extra_id);
		json.put("root_tid", t.root_tid);
		json.put("post_time", t.post_time);
		json.put("from_string", t.from_string);
		json.put("weibo_tid", t.weibo_tid);
		json.put("request_id", t.request_id);
		json.put("replys", t.replys);
		json.put("forwards", t.forwards);
		json.put("glances", t.glances);
		json.put("praised", t.isPraised());
		json.put("praises", t.praises);
		json.put("article", t.article);
		if(null != t.user){
			json.put("user",new UserInfoParser().toJSONObject(t.user));
		}
		if(null != t.attach){
			json.put("attach", new AttachListParser().toJSONArray(t.attach));
		}
		if(null != t.image){
			json.put("image", new ImageListparser().toJSONArray(t.image));
		}
		if(t.type != null && t.type.compareTo(TopicInfo.TYPE_FORWARD) == 0){
			json.put("root_topic", new RootTopicParser().toJSONObject(t.root_topic));
		}
		if(t.category != null && t.category.compareTo(TopicInfo.CATEGORY_RANK) == 0){
		    json.put("ranking_user", new UserInfoListParser().toJSONArray(t.rankingUserList));
		}
		return json;
	}

}
